package com.business.system.action;/**
 * Created by Honker on 2018/4/17.
 */

import com.business.system.util.DBTools;
import com.business.system.util.PageUtil;
import net.sf.rose.jdbc.PageBean;
import net.sf.rose.jdbc.service.Service;
import net.sf.rose.web.utils.WebUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 订单管理相关类
 * @author zpc
 * @create 2018-04-17 20:37
 **/
@RestController
@RequestMapping("order")
public class OrderAction {
    /**
     * 获取订单列表
     * @param service
     * @param request
     * @return
     */
    @RequestMapping("getOrderList.do")
    public PageBean getOrderList(Service service, HttpServletRequest request) throws ParseException {
        PageBean page = PageUtil.page(request);
        StringBuilder sql = new StringBuilder();
        sql.append("select * from goods_orders where 1=1");
        Map<String, Object> params = WebUtils.getRequestData(request);
        List<Object> condition = new ArrayList<>();
        SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (params.containsKey("content")) {
            sql.append(" and (ORDER_NO like '%" + params.get("content") + "%' )");
        }
        if (params.containsKey("id")) {
            sql.append(" and (ID = '" + params.get("id") + "' )");
        }
        if (params.containsKey("beginTime")) {

            sql.append(" and (ORDER_TIME >= '" + params.get("beginTime").toString() + " 00:00:00' )");
        }
        if (params.containsKey("endTime")) {
            sql.append(" and (ORDER_TIME <= '" + params.get("endTime") + " 23:59:59') ");
        }
        if (params.containsKey("status")) {
            if(!"0".equals(params.get("status").toString()))
                sql.append(" and (STATUS = '" + params.get("status") + "' )");
        }
        sql.append(" order by ORDER_TIME desc");
        DBTools.getDataList(service, sql.toString(), page);
        List<Object> data = page.getData();
        return page;
    }

    /**
     * 订单商品列表
     * @param service
     * @param request
     * @return
     */
    @RequestMapping("getOrderGoodsList.do")
    public List<Map<String,Object>> getOrderGoodsList(Service service, HttpServletRequest request){
        StringBuilder sql = new StringBuilder();
        sql.append("select * from order_goods as t1 left join goods as t2 on t1.GOODS_ID=t2.ID where 1=1");
        Map<String, Object> params = WebUtils.getRequestData(request);
        List<Object> condition = new ArrayList<>();
        if (params.containsKey("ordersId")) {
            sql.append(" and (t1.orders_id= ? )");
            condition.add(params.get("ordersId"));
        }
        return DBTools.getDataList(service,sql.toString(),condition);
    }

    /**
     * 订单发货更新
     * @param service
     * @param request
     * @return
     */
    @RequestMapping("updateOrderGoods.do")
    public boolean updateOrderGoods(Service service, HttpServletRequest request){
        StringBuilder sql = new StringBuilder();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date day=new Date();
        String now = df.format(day);
        sql.append(" update goods_orders set STATUS =3,DELIVERY_TIME = '"+now+"',EXPRESS_TYPE=?,EXPRESS_NO=? where ID=?");
        Map<String, Object> params = WebUtils.getRequestData(request);
        int num = DBTools.updateSql(service,sql.toString(),params.get("EXPRESS_TYPE"),params.get("EXPRESS_NO"),params.get("ID"));
        return num>0;
    }


    /**
     * 获取月销售统计数据
     * @param service
     * @param request
     * @return
     */
    @RequestMapping("getOrderStatisticList.do")
    public PageBean getOrderStatisticList(Service service, HttpServletRequest request) throws ParseException {
        PageBean page = PageUtil.page(request);
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT tt.* FROM (  SELECT t.TIME,t.GOODS_NO,t.GOODS_NAME,t.BRAND_NAME,SUM(t.COUNTS) as COUNTS,FORMAT(SUM(t.COST),2) AS COST,FORMAT(SUM(t.PROFIT),2) AS PROFIT FROM( ");
        sql.append(" SELECT  date_format(go.ORDER_TIME,'%Y-%m') as TIME, gs.GOODS_NO,gs.GOODS_NAME,bd.BRAND_NAME,og.COUNTS,og.COUNTS*og.SALE_PRICE AS COST,og.COUNTS*(og.SALE_PRICE-gs.COST_PRICE) AS PROFIT  FROM goods_orders as go ");
        sql.append(" LEFT JOIN order_goods as og on og.ORDERS_ID = go.ID ");
        sql.append(" LEFT JOIN goods as gs on gs.ID = og.GOODS_ID ");
        sql.append(" LEFT JOIN brand as bd on bd.ID = gs.BRAND_ID ");
        sql.append(" WHERE 1=1 AND go.`STATUS`>=2 ");
        Map<String, Object> params = WebUtils.getRequestData(request);
        List<Object> condition = new ArrayList<>();
        SimpleDateFormat time = new SimpleDateFormat("yyyy-MM");
        if (params.containsKey("content")) {
            sql.append(" and (gs.GOODS_NO  like ?  or gs.GOODS_NAME LIKE ? )");
            condition.add("%"+params.get("content")+"%");
            condition.add("%"+params.get("content")+"%");
        }
        if (params.containsKey("beginTime")) {

            sql.append(" and (go.ORDER_TIME>= ?)");
            condition.add(params.get("beginTime").toString()+"-01 00:00:00");
        }
        if (params.containsKey("endTime")) {
            sql.append(" and (go.ORDER_TIME<= ?)");
            condition.add(params.get("endTime").toString()+"-31 24:59:59");
        }
        if (params.containsKey("brand")) {
            if(!"-1".equals(params.get("brand").toString())){
                sql.append(" and ( bd.ID= ?)");
                condition.add(params.get("brand"));
            }

        }
        sql.append("  ) as t GROUP BY t.TIME,t.GOODS_NO ) as tt ");
        return  DBTools.getDataList(service, sql.toString(), page,condition);
    }
}
